package com.passwd.designencrypt.encrypt;

import com.passwd.designcore.utils.KeyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
public class EncryptController {

    public static void main(String[] args) {
        encryptLoad();
    }
    /**
     * 接收加密请求
     * 按照规定读取路径读取文件
     *
     * @return
     */
    @RequestMapping("encrypt")
    public static String encryptLoad() {
        String result = "error";
        try {
            String path = System.getProperty("user.dir");
            File dir = new File(path);
            //TODO 路径待写
            File file = new File(dir, KeyUtils.inputEncryptFileName);

            // 核心处理类
            EncryptCore core = new EncryptCore();
            result = core.encryptRun(new FileInputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage();
        }
        return result;
    }

    /**
     * 接收加密请求
     * 上传文件形式读取文件
     *
     * @return
     */
    @RequestMapping("encryptUpload")
    public String encryptUpload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        InputStream inputStream = file.getInputStream();

        // 核心处理类
        EncryptCore core = new EncryptCore();
        String result = core.encryptRun(inputStream);
        return result;
    }
    /*




    */
}
